package com.zjsru.oneDay;

import java.util.ArrayList;
import java.util.List;

/***
 *
 * 矩阵中的幸运数
 * 输入：matrix = [[3,7,8],[9,11,13],[15,16,17]]
 *  * 输出：[15]
 *  * 解释：15 是唯一的幸运数，因为它是其所在行中的最小值，也是所在列中的最大值。
 *  *
 *
 *
 */
public class luckyNumbers {
    public List<Integer> luckyNumbers(int[][] matrix) {
        int col = matrix.length;
        int row = matrix[0].length;
        List<Integer> ret = new ArrayList<Integer>();
        for (int i = 0; i < col; i++) {
            for (int j = 0; j < row; j++) {
                boolean isMin = true, isMax = true;
                for (int k = 0; k < row; k++) {
                    if (matrix[i][k] < matrix[i][j]) {
                        isMin = false;
                        break;
                    }
                }
                if (!isMin) {
                    continue;
                }
                for (int k = 0; k < col; k++) {
                    if (matrix[k][j] > matrix[i][j]) {
                        isMax = false;
                        break;
                    }
                }
                if (isMax) {
                    ret.add(matrix[i][j]);
                }
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        luckyNumbers luckyNumbers = new luckyNumbers();
        int[][] matrix = new int[][]{{3, 7, 8}, {9, 11, 13}, {15, 16, 17}};
        System.out.println(luckyNumbers.luckyNumbers(matrix));
    }
}
